home *** CD-ROM | disk | FTP | other *** search
/ PC World 2007 June / PCWorld_2007-06_cd.bin / v cisle / tclock / tclocklight-040702-3.exe / source / exe / alarm.c < prev    next >
C/C++ Source or Header  |  2004-05-11  |  4KB  |  197 lines

  1. /*-------------------------------------------------------------
  2.   alarm.c : process alarms
  3.   (C) 1997-2003 Kazuto Sato
  4.   Please read readme.txt about the license.
  5.   
  6.   Written by Kazubon, Nanashi-san
  7. ---------------------------------------------------------------*/
  8.  
  9. #include "tclock.h"
  10.  
  11. /* Globals */
  12.  
  13. void InitAlarm(void);
  14. void EndAlarm(void);
  15. void OnTimerAlarm(HWND hwnd, const SYSTEMTIME* st, int reason);
  16.  
  17. /* Statics */
  18.  
  19. static PALARMSTRUCT m_pAlarm = NULL;
  20. static int m_numAlarm = 0;
  21. static BOOL m_bCheckEverySeconds = FALSE;
  22.  
  23. /*------------------------------------------------
  24.   initialize
  25. --------------------------------------------------*/
  26. void InitAlarm(void)
  27. {
  28.     PALARMSTRUCT pAS;
  29.     int i;
  30.     int jihou;
  31.     
  32.     m_numAlarm = GetMyRegLong("", "AlarmNum", 0);
  33.     if(m_numAlarm < 1) m_numAlarm = 0;
  34.     
  35.     if(m_pAlarm) free(m_pAlarm);
  36.     m_pAlarm = NULL;
  37.     
  38.     jihou = 0;
  39.     if(GetMyRegLong("", "Jihou", FALSE)) jihou = 1;
  40.     
  41.     if(m_numAlarm + jihou == 0) return;
  42.     
  43.     m_pAlarm = malloc(sizeof(ALARMSTRUCT) * (m_numAlarm + jihou));
  44.     
  45.     // read settings
  46.     LoadAlarm(m_pAlarm, m_numAlarm); // common/alarmstruct.c
  47.     
  48.     // cuckoo clock
  49.     if(jihou)
  50.     {
  51.         pAS = m_pAlarm + m_numAlarm;
  52.         m_numAlarm++;
  53.         
  54.         memset(pAS, 0, sizeof(ALARMSTRUCT));
  55.         strcpy(pAS->strHours, "*");
  56.         strcpy(pAS->strMinutes, "0");
  57.         strcpy(pAS->strWDays, "*");
  58.         SetAlarmTime(pAS); // common/alarmstruct.c
  59.         
  60.         pAS->bEnable = TRUE;
  61.         pAS->bHour12 = TRUE;
  62.         GetMyRegStr("", "JihouFile", pAS->fname, MAX_PATH, "");
  63.         if(GetMyRegLong("", "JihouRepeat", FALSE))
  64.             pAS->bRepeatJihou = TRUE;
  65.         if(GetMyRegLong("", "JihouBlink", FALSE))
  66.         {
  67.             pAS->bBlink = TRUE; pAS->nBlinkSec = 60;
  68.         }
  69.     }
  70.     
  71.     m_bCheckEverySeconds = FALSE;
  72.     if(m_pAlarm)
  73.     {
  74.         for(i = 0; i < m_numAlarm; i++)
  75.         {
  76.             pAS = m_pAlarm + i;
  77.             if(pAS->bEnable)
  78.             {
  79.                 if(pAS->second)
  80.                     m_bCheckEverySeconds = TRUE;
  81.                 else if(pAS->bInterval)
  82.                 {
  83.                     if(!pAS->bBootExec)
  84.                         pAS->tickLast = GetTickCount();
  85.                     m_bCheckEverySeconds = TRUE;
  86.                 }
  87.             }
  88.         }
  89.     }
  90. }
  91.  
  92. /*------------------------------------------------
  93.   clear up
  94. --------------------------------------------------*/
  95. void EndAlarm(void)
  96. {
  97.     if(m_pAlarm) free(m_pAlarm);
  98.     m_pAlarm = NULL;
  99. }
  100.  
  101. /*------------------------------------------------
  102.   execute alarms
  103. --------------------------------------------------*/
  104. void OnTimerAlarm(HWND hwnd, const SYSTEMTIME* st, int reason)
  105. {
  106.     PALARMSTRUCT pAS;
  107.     static int hourLast = 0, minuteLast = 0;
  108.     int i, hour, loops;
  109.     
  110.     if(!m_pAlarm) return;
  111.     
  112.     // execute once a minute
  113.     if(reason == 0 && st && !m_bCheckEverySeconds)
  114.     {
  115.         if(hourLast == (int)st->wHour &&
  116.             minuteLast == (int)st->wMinute) return;
  117.         hourLast = st->wHour;
  118.         minuteLast = st->wMinute;
  119.     }
  120.     
  121.     for(i = 0; i < m_numAlarm; i++)
  122.     {
  123.         BOOL bexec = FALSE;
  124.         
  125.         pAS = m_pAlarm + i;
  126.         if(!pAS->bEnable) continue;
  127.         
  128.         // 12 hour
  129.         hour = 0;
  130.         if(st)
  131.         {
  132.             hour = st->wHour;
  133.             if(pAS->bHour12)
  134.             {
  135.                 if(hour == 0) hour = 12;
  136.                 else if(hour >= 13) hour -= 12;
  137.             }
  138.         }
  139.         
  140.         // compare time
  141.         if(reason == 0 && st)
  142.         {
  143.             if(pAS->hours[hour]
  144.                 && pAS->minutes[st->wMinute]
  145.                 && pAS->wdays[st->wDayOfWeek])
  146.             {
  147.                 if(pAS->second)
  148.                 {
  149.                     if(pAS->second == st->wSecond) bexec = TRUE;
  150.                 }
  151.                 else bexec = TRUE;
  152.             }
  153.         }
  154.         
  155.         // Execute when TClock is started
  156.         if(reason == 1)
  157.         {
  158.             if(pAS->bBootExec) bexec = TRUE;
  159.         }
  160.         
  161.         // At regular intervals
  162.         if(st && pAS->bInterval && pAS->nInterval > 0)
  163.         {
  164.             if(pAS->hours[hour] && pAS->wdays[st->wDayOfWeek])
  165.             {
  166.                 if(GetTickCount() - pAS->tickLast >
  167.                     (DWORD)pAS->nInterval * 1000 * 60) bexec = TRUE;
  168.             }
  169.         }
  170.         
  171.         // not to execute an alarm twice within a minute
  172.         if(bexec)
  173.         {
  174.             DWORD tick = GetTickCount();
  175.             if(tick - pAS->tickLast < 60000 && reason == 0)
  176.                 bexec = FALSE;
  177.             else pAS->tickLast = tick;
  178.         }
  179.         
  180.         if(bexec)
  181.         {
  182.             if(pAS->bBlink && g_hwndClock)
  183.                 PostMessage(g_hwndClock, CLOCKM_BLINK, 0, pAS->nBlinkSec);
  184.             
  185.             if(pAS->fname[0])
  186.             {
  187.                 if(pAS->bRepeat) loops = -1; else loops = 0;
  188.                 if(pAS->bRepeatJihou) loops = hour;
  189.                 
  190.                 // common/playfile.c
  191.                 PlayFile(hwnd, pAS->fname, loops);
  192.             }
  193.         }
  194.     }
  195. }
  196.  
  197.